// component/news_common/seckill-box/seckill-box.js
const app=getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    seckillList:{
      type:Array,
      value:[],
      observer(newVal, oldVal, changedPath) {
          if(newVal.length>0){
            this.into();
            this.setData({
              time:app.functions.OffGroup()
            })
          }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    theme:app.theme,
    seckillText:app.iscreditssf,
    time: 0,
    timeData: {}
  },
  // 当切换模式改变
  attached(){
    this.into();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    into(){
      this.setData({
        theme:app.theme,
        seckillText:app.iscreditssf
      })
    },
    onChange(e) {
      if (e.detail.hours >= 1 && e.detail.hours <= 9) {
        e.detail.hours  = "0" + e.detail.hours;
      }
      if (e.detail.minutes >= 0 && e.detail.minutes <= 9) {
        e.detail.minutes  = "0" + e.detail.minutes;
      }	
      if (e.detail.seconds >= 0 && e.detail.seconds <= 9) {
        e.detail.seconds  = "0" + e.detail.seconds;
      }	

      this.setData({
        timeData: e.detail
      });
    },
    // 有规格的商品调用规格
    onaddCart(e){
      let goods=e.detail.goods;
      let index=e.detail.index;
      let platform=e.detail.platform;
      if(platform=='self'){
        this.selectComponent("#suk").getProductsDetail(goods.id,index,this.data.supplyChannel);
      }
    },
    ajaxAddCart(index,goods,plus=true){
      let that=this;
      let userinfo = app.functions.getLocalStorage("user");
      let shop=app.functions.getLocalStorage("shop");
      if(app.functions.isEmpty(userinfo)){
        app.functions.islogin();
        return false;
      }
       let cart={
        supplyChannel:2,
        mid:userinfo.id,
        teamid:shop.id,
        productId:goods.id,
        total: plus ? 1 : -1,
        marketprice: goods.marketprice
      };
      //修改吉贡单规格商品
      if(goods.skus.length==1) {
        let skusOne = goods.skus[0];
        cart.skuId = skusOne.id;
        cart.skuTitle = skusOne.title;
        cart.marketprice = (user.isVip && skusOne.vipprice && goods.isvip) ? skusOne.vipprice : skusOne.marketprice;
        cart.vipprice = skusOne.vipprice;
      }

      if (goods.maxbuy && (goods.cartNum + cart.total) > goods.maxbuy) {
        app.functions.showToast("已经超过单次购买数量");
        return;
      }
      app.api.cartAdd(cart,(response)=>{
        if(response.code!=0){
          app.functions.showToast("加入到购物车失败");
          return ;
        }
        app.functions.showToast("已加入到购物车");
        app.cartNumber = app.cartNumber == 0 ? cart.total : parseInt(app.cartNumber) + cart.total;
        that.setData({
          ["items.cartNum"]:this.data.items.cartNum+cart.total
        })
        app.UpdateCart();
      },(error)=>{

      })
    },
    toSeckillList(){
      wx.navigateTo({
        url:"/pages_subpack/seckill_list/seckill_list"
      })
    }
  }
})
